package com.weijia;

import javax.swing.*;
import java.awt.*;
import java.util.List;

public class GamePanel extends JPanel {
    private GameManager gameManager;
    public void setGameManager(GameManager gameManager) {
        this.gameManager = gameManager;
    }
    public GamePanel() {
        setBackground(new Color(30, 30, 50));
        setPreferredSize(new Dimension(800, 700));
        setLayout(null); // 暂时不使用 OverlayLayout，后续可调整
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (gameManager == null) return;
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        // 绘制网格背景
        g2d.setColor(new Color(40, 40, 60));
        for (int i = 0; i < getWidth(); i += 40) {
            g2d.drawLine(i, 0, i, getHeight());
        }
        for (int i = 0; i < getHeight(); i += 40) {
            g2d.drawLine(0, i, getWidth(), i);
        }
        // 绘制标题
        g2d.setColor(new Color(100, 200, 255));
        g2d.setFont(new Font("SansSerif", Font.BOLD, 36));
        String title = "TypingGamePlus";
        int titleWidth = g2d.getFontMetrics().stringWidth(title);
        g2d.drawString(title, (getWidth() - titleWidth) / 2, 50);
        // 绘制难度信息
        g2d.setColor(new Color(255, 215, 100));
        g2d.setFont(new Font("SansSerif", Font.BOLD, 24));
        String difficultyText = "难度: " + gameManager.getDifficulty().name().toLowerCase().replace("_", " ").toUpperCase().charAt(0) +
                gameManager.getDifficulty().name().toLowerCase().replace("_", " ").substring(1);
        int diffWidth = g2d.getFontMetrics().stringWidth(difficultyText);
        g2d.drawString(difficultyText, (getWidth() - diffWidth) / 2, 90);
        // 绘制游戏说明
        g2d.setColor(new Color(150, 220, 255));
        g2d.setFont(new Font("SansSerif", Font.PLAIN, 16));
        String instruction = "在右侧输入下落的单词，按回车确认";
        int insWidth = g2d.getFontMetrics().stringWidth(instruction);
        g2d.drawString(instruction, (getWidth() - insWidth) / 2, 130);
        // 绘制当前分数
        g2d.setColor(new Color(255, 215, 100));
        g2d.setFont(new Font("SansSerif", Font.BOLD, 24));
        g2d.drawString("当前分数: " + gameManager.getScore(), 30, 40);
        // 绘制下落的单词
        List<FallingWord> activeWords = gameManager.getActiveWords();
        for (FallingWord fw : activeWords) {
            drawFallingWord(g2d, fw);
        }
        // 游戏暂停时显示提示
        if (gameManager.isGamePaused()) {
            g2d.setColor(new Color(0, 0, 0, 150));
            g2d.fillRect(0, 0, getWidth(), getHeight());
            g2d.setColor(Color.WHITE);
            g2d.setFont(new Font("SansSerif", Font.BOLD, 48));
            String pausedText = "游戏已暂停";
            int textWidth = g2d.getFontMetrics().stringWidth(pausedText);
            g2d.drawString(pausedText, (getWidth() - textWidth) / 2, getHeight() / 2 - 30);
            g2d.setFont(new Font("SansSerif", Font.PLAIN, 24));
            String continueText = "点击继续或按暂停按钮";
            int continueWidth = g2d.getFontMetrics().stringWidth(continueText);
            g2d.drawString(continueText, (getWidth() - continueWidth) / 2, getHeight() / 2 + 30);
        }
    }

    private void drawFallingWord(Graphics2D g2d, FallingWord fw) {
        g2d.setColor(new Color(0, 0, 0, 100));
        g2d.fillRoundRect(fw.x - 5, fw.y - 25, g2d.getFontMetrics().stringWidth(fw.word) + 10, 40, 20, 20);
        g2d.setColor(fw.color);
        g2d.setFont(new Font("SansSerif", Font.BOLD, 24));
        g2d.drawString(fw.word, fw.x, fw.y);
        g2d.setColor(new Color(255, 200, 100));
        g2d.fillRect(fw.x, fw.y + 5, fw.speed * 10, 3);
    }
} 